<?php
/**
 * 平台月销售量 (平台基础月销售量数据)
 */
namespace App\Logic\SalesAmazon;

use App\Models\Basics\SysPlat;
use App\Models\Product\ProductAmazonSku;
use App\Models\Product\ProductAmazonSkuCCS;
use App\Models\Product\ProductRakutenSku;
use App\Models\Product\ProductSku;
use App\Models\SalesAmazon\SalesVolumeDay;
use App\Models\SalesAmazon\SalesVolumeMonth;
use Illuminate\Support\Facades\DB;
class SalesVolumeMonthly
{
    public static function init(){

        $amazonPlat = SysPlat::PLAT_AMAZON; //amazon平台
        $shopifyPlat = SysPlat::PLAT_SHOPIFY; //shopify平台
        $rakutenPlat = SysPlat::PLAT_RAKUTEN; //rakuten平台

        $day = date('j');
        if($day == 1){
            //月初第一次，执行上一月数据
            $time = strtotime("midnight first day of -1 month");
            $dates = [
                'start' => date('Ym01',$time),
                'end'   => date('Ymt',$time),
                'month' => date('Ym',$time)
            ];
        }else{
            $dates = [
                'start' => date('Ym01'),
                'end'   => date('Ymd'),
                'month' => date('Ym')
            ];
        }

        //二.平台月销量(月份)
        $_start = $dates['start'];
        $_end  = $dates['end'];
        $month = $dates['month'];
        $classify = [];
        $seller = [];
        SalesVolumeDay::query()
        ->whereBetween('day', [$_start, $_end])
        ->select('plat_id', 'country_id', 'seller_sku', DB::raw('sum(volume) as volume'))
        ->groupBy(['plat_id', 'country_id', 'seller_sku'])
        ->chunk(100, function ($items) use (&$classify,&$seller,$month,$amazonPlat,$shopifyPlat,$rakutenPlat){
            foreach ($items as $item){
                $model = SalesVolumeMonth::query()
                    ->where('plat_id', $item->plat_id)
                    ->where('country_id', $item->country_id)
                    ->where('seller_sku', $item->seller_sku)
                    ->where('month', $month)
                    ->first();
                if ($model) {
                    $model->volume = $item->volume;
                    $model->save();
                }else {
                    $pSkuId = 0;
                    //amazon产品
                    if ($item->plat_id == $amazonPlat) {
                        $_pSkuId = ProductAmazonSku::query()->where('seller_sku', $item->seller_sku)->value('psku_id');
                        $pSkuId = $_pSkuId ?? 0;
                    }
                    //shopify产品
                    if ($item->plat_id == $shopifyPlat) {
                        //$_pSkuId = ProductShopifySku::query()->where('seller_sku', $item->seller_sku)->value('psku_id');
                        //(因shopify产品尚无人维护，暂且使用公司产品取值。)
                        $_pSkuId = ProductSku::query()->where('psku_code', $item->seller_sku)->value('id');
                        $pSkuId = $_pSkuId ?? 0;
                    }
                    //rakuten产品
                    if ($item->plat_id == $rakutenPlat) {
                        $_pSkuId = ProductRakutenSku::query()->where('seller_sku', $item->seller_sku)->value('psku_id');
                        $pSkuId = $_pSkuId ?? 0;
                    }

                    //产品品类
                    if (!array_key_exists($pSkuId, $classify)) {
                        $_classifyId = ProductSku::getValueById($pSkuId, 'classify_id');
                        $classify[$pSkuId] = $_classifyId ?: 0;
                    }
                    $classifyId = $classify[$pSkuId] ?? 0;

                    //amazon销售人员
                    if ($item->plat_id == $amazonPlat) {
                        if (!array_key_exists($classifyId, $seller)) {
                            $seller[$classifyId] = ProductAmazonSkuCCS::getSellerId($item->country_id, $classifyId);
                        }
                    }
                    $sellerId = $seller[$classifyId] ?? 0;

                    //shopify销售人员
                    if ($item->plat_id == $shopifyPlat) $sellerId = 33; //joyce
                    //rakuten销售人员
                    if ($item->plat_id == $rakutenPlat) $sellerId = 124; //henter

                    //(平台)月销量
                    SalesVolumeMonth::query()->create([
                        'plat_id' => $item->plat_id,
                        'country_id' => $item->country_id,
                        'seller_sku' => $item->seller_sku,
                        'month' => $month,
                        'classify_id' => $classifyId,
                        'seller_id' => $sellerId,
                        'volume' => $item->volume,
                    ]);
                }
            }
        });
    }

}
